﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TravelingWebApp.Models;
using TravelingWebApp.Data;
using System.IO;

namespace TravelingWebApp.Repositories
{
    public class RouteRepositories : IRouteRepositories
    {
        private TravelingContext _ctx;

        public RouteRepositories(TravelingContext ctx)
        {
            _ctx = ctx;
        }

        public IEnumerable<Route> GetRoutes()
        {
            return _ctx.DbRoutes.ToList();
        }

        public Route GetRouteById(string id)
        {
            return _ctx.DbRoutes.Include(b => b.Id)
                .SingleOrDefault(c => c.Id == id);
        }

        public void AddRoute(Route route)
        {
            _ctx.Add(route);
            _ctx.SaveChanges();
        }

        public void DeleteRouteById(string id)
        {
            var route = _ctx.DbRoutes.SingleOrDefault(c => c.Id == id);
            _ctx.DbRoutes.Remove(route);
            _ctx.SaveChanges();
        }

        public void SaveChanges()
        {
            _ctx.SaveChanges();
        }

    }
}
